<?php 

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Jan. 12, 2009

//Initiate classes
$page = new init;
$theme = new themeManager($page->page_info['id']);
//Use clean URLS
if($page->getSetting('Clean Urls') == "Enabled") {
	$clean = new CleanURL;
	$clean->parseURL(); 
	$clean->setParts('title', 'page'); 
}
//Setup Smarty Templating System
$smarty->left_delimiter = '{{';
$smarty->right_delimiter = '}}';
$smarty->template_dir = "";
$smarty->compile_dir = BASE_URL.'temp/';
$smarty->cache_dir = BASE_URL.'temp/';
$smarty->config_dir = BASE_URL.'includes/smarty/configs';


//Get the requested page id
global $pageid;
if($_GET['page'] && is_numeric($_GET['page'])) {
	$page->page_info['id'] = $_GET['page'];
	$GLOBALS['pageid'] = $_GET['page'];
}
elseif ($_GET['page'] == "login") {
	$page->pageContent($adminLoginForm);
	$page->page_info['content']['page_title'] = "Login";
}
else {
	$page->page_info['id'] = $page->page_info['settings']['Home Page Id']['value'];
	$GLOBALS['pageid'] = $page->page_info['settings']['Home Page Id']['value'];
}

if($_POST['login']) {
	session_start();
	$_SESSION['username'] = sanitize_sql_string($_POST['username']);
	$_SESSION['password'] = sha1(sanitize_sql_string($_POST['password']));
	$page->validate("", BASE_URL."admin.php");
	if($page->validate() == false) {
		$_SESSION = array();
		session_destroy();
		$page->pageContent($adminLoginForm, 0);	
		$page->pageContent("<style type='text/css'>@import '".BASE_URL."includes/admin/css/messages.css';</style>".$page->message('error', 'Login Failed', 'Invalid user name or password. Please try again.'), -1);	
	}	
}

//functions used in the templates

$GLOBALS['page'] = $page;
$GLOBALS['theme'] = $theme;
function navigation($a = 'ul-navigation', $b = 'ul-sub-navigation', $c = 'selected', $d = true) {
	$return =&  $GLOBALS['theme']->navigation($a,$b,$c,$d);
	return $return;
}
function secondaryNavigation($a = 'ul-sec-navigation', $b = 'ul-sec-sub-navigation', $c= 'selected', $d =true) {
	$return =&  $GLOBALS['theme']->secondary_navigation($a,$b,$c,$d);
	return $return;
}
function staticContent($a, $b, $c = 'no') {
	$return =&  $GLOBALS['theme']->static_content($a,$b,$c);
	return $return;
}
function getSetting($a = 'Site Name') {
	return $GLOBALS['page']->getSetting($a);
}
function pageContent () {
	return $GLOBALS['page']->page_info['content']['page_content'];
}
function pageTitle () {
	return $GLOBALS['page']->page_info['content']['page_title'];
}


//Prepare the settings arrays to be Smarty safe
$page->prepareSettingArraysSmarty();

//Assign Smarty Templating Sytem values
$theme_url = THEME_URL.'themes/'.trim($page->page_info['settings']['Theme']['value']).'/';
$base_url = THEME_URL;
$pageid = $pageid;


	//Assign settings arrays
	//$smarty->assign('settings', $page_settings);
	//$smarty->assign('page_info', $page->page_info);
	//$smarty->assign('system', $page_settings_system);
	
	//Assign content array
	//$smarty->assign('content', $page->page_info['content']);

//Assign page loading time
$page_generation_time = round(microtime(), 4) - PAGE_LOAD_START + 0.0025;

//Display page with Smarty
include(BASE_URL.'themes/'.trim($page->page_info['settings']['Theme']['value']).'/index.tpl.php');

//Pre-release version raw print out of global array
if($_GET['debug']) {
	echo $page->arrayToUl($page->page_info);
	echo '<div class="debug" style="padding:17px;background:black;opacity:0.9;color:white;position:fixed;bottom:0;left:0;height:100px;overflow:auto;width:98%;font-family:Consolas;">'.$GLOBALS['mysql_debug'].'</div>';
	echo '<div class="debug" style="padding:17px;background:black;opacity:0.9;color:white;position:fixed;top:0;left:0;height:100px;overflow:auto;width:98%;font-family:Consolas;">'.$GLOBALS['settingsDebug'].'</div>';
}
?>